package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Clazz;
import com.sky.domain.Teacher;
import com.sky.service.ClazzService;
import com.sky.service.TeacherService;
import com.sky.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 尹稳健~
 * @description: 控制器-管理教师信息页面
 * @version 1.0
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    //注入业务对象
    @Autowired
    @Qualifier("teacherServiceImpl")
    private TeacherService teacherService;

    //注入业务对象
    @Autowired
    @Qualifier("clazzServiceImpl")
    private ClazzService clazzService;

    //存储预返回页面的数据对象
    private Map<String,Object> result = new HashMap<String,Object>();

    /**
     * @description: 跳转到教师信息管理页面
     * @param: org.springframework.ui.Model
     * @return: java.lang.String
     */
    @RequestMapping("/goTeacherListPage")
    public String goTeacherListPage(Model model){
        List<Clazz> clazzList = clazzService.selectAll();
        //向页面发送一个存储着Clazz的List对象
        model.addAttribute("clazzList",clazzList);
        return "/teacher/teacherList";
    }

    /**
     * @description: 分页查询学生信息列表:根据教师与班级名查询指定/全部教师信息列表
     * @param: page 当前页码
     * @param: rows 列表行数
     * @param: teacherName
     * @param: clazzName
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/getTeacherList")
    @ResponseBody
    public Map<String,Object> getTeacherList(Integer page,Integer rows,
                                             String clazzName,String name){
        Teacher teacher = new Teacher(name, clazzName);
        // 设置每页的记录数
        PageHelper.startPage(page,rows);
        // 根据班级与教师名获取指定或全部教师信息列表
        List<Teacher> list = teacherService.getTeacherList(teacher);
        // 获取封装信息
        PageInfo<Teacher> pageInfo = new PageInfo<Teacher>(list);
        // 获取当前页记录数
        List<Teacher> teacherList = pageInfo.getList();
        // 获取总记录数
        long total = pageInfo.getTotal();
        // 存储数据对象
        result.put("total",total);
        result.put("rows",teacherList);
        return result;
    }

    /**
     * @description: 添加教师信息
     * @param: teacher
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/addTeacher")
    @ResponseBody
    public Map<String,Object> addTeacher(Teacher teacher){
        Teacher teacherById = teacherService.getTeacherById(teacher);
        //判断工号是否已存在
        if (teacherById!=null){
            result.put("success",false);
            result.put("msg","该工号已经存在! 请修改后重试!");
            return result;
        }
        // 添加操作
        teacherService.addTeacher(teacher);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 根据id修改指定的教师信息
     * @param: teacher
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/editTeacher")
    @ResponseBody
    public Map<String,Object> editTeacher(Teacher teacher){
        teacherService.editTeacher(teacher);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 根据id删除指定的教师信息
     * @param: ids
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/deleteTeacher")
    @ResponseBody
    public Map<String,Object> deleteTeacher(@RequestParam(value = "ids[]" ,required = true) Integer[] ids){
        teacherService.deleteTeacher(ids);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 上传头像-原理:将头像上传到项目发布目录中,通过读取数据库中的头像路径来获取头像
     * @param: photo
     * @param: request
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/uploadPhoto")
    @ResponseBody
    public Map<String,Object> uploadPhoto(MultipartFile photo, HttpServletRequest request){
        //存储头像的本地目录
        final String dirPath = request.getRealPath("/upload/teacher_portrait/");
        final String portraitPath = request.getContextPath()+"/upload/teacher_portrait/";
        //返回头像的上传结果
        return UploadFile.getUploadResult(photo,dirPath,portraitPath);
    }
}
